package com.objectcarpentry.kaylee.ui.views;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.ui.dnd.LocalTransfer;
import org.eclipse.emf.edit.ui.dnd.ViewerDragAdapter;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import com.objectcarpentry.kaylee.AbstractPlaylist;
import com.objectcarpentry.kaylee.IPlayable;
import com.objectcarpentry.kaylee.IPlayer;
import com.objectcarpentry.kaylee.IPlaylist;
import com.objectcarpentry.kaylee.KayleePackage;
import com.objectcarpentry.kaylee.KayleePlugin;
import com.objectcarpentry.kaylee.provider.KayleeEditPlugin;
import com.objectcarpentry.kaylee.provider.KayleeItemProviderAdapterFactory;
import com.objectcarpentry.kaylee.provider.LibraryItemProvider;
import com.objectcarpentry.kaylee.ui.KayleeUIListener;
import com.objectcarpentry.kaylee.ui.KayleeUIPlugin;

public class TracklistView extends ViewPart implements KayleeUIListener {

	public static final String ID = "com.objectcarpentry.kaylee.ui.views.tracklist";

	protected TableViewer viewer;

	@Override
	public void createPartControl(Composite parent) {
		
		viewer = new TableViewer(parent);
		viewer.getTable().setLinesVisible(true);
		viewer.getTable().setHeaderVisible(true);
	
		
		ResourceLocator locator = KayleeEditPlugin.getPlugin();
		String[] columnNames = new String[] { 
				"",
				locator.getString("_UI_Track_artist_feature"),
				locator.getString("_UI_Track_title_feature"),
		};
			
		int[] columnWidths = new int[] { 20, 200, 200, };

		for (int i = 0; i < columnNames.length; i++) {
			  TableColumn tableColumn = new TableColumn(viewer.getTable(), SWT.LEFT);
			  tableColumn.setText(columnNames[i]);
			  tableColumn.setWidth(columnWidths[i]);
		}

		KayleeItemProviderAdapterFactory factory = new KayleeItemProviderAdapterFactory() {

			@Override
			public Adapter createLibraryAdapter() {
				if (libraryItemProvider == null) {
					libraryItemProvider = new LibraryItemProvider(this) {
						@Override
						public Collection<? extends EStructuralFeature> getChildrenFeatures(
								Object object) {
							if (childrenFeatures == null) {
								childrenFeatures = new ArrayList<EStructuralFeature>();
								childrenFeatures.add(KayleePackage.Literals.LIBRARY__ELEMENTS);
							}
							return childrenFeatures;
						}
					};
				}
				return libraryItemProvider;
			}
		};

		AdapterFactoryContentProvider contentProvider = new AdapterFactoryContentProvider(factory);
		AdapterFactoryLabelProvider labelProvider = new AdapterFactoryLabelProvider.FontAndColorProvider(factory,viewer);
		
		viewer.setContentProvider(contentProvider);
		viewer.setLabelProvider(labelProvider);
		
		
		int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
		Transfer[] transfers = new Transfer[] { LocalTransfer.getInstance() };
		viewer.addDragSupport(dndOperations, transfers, new ViewerDragAdapter(viewer));
			
		hookMouseListener();
		getSite().setSelectionProvider(viewer);
		
		showPlaylist(KayleeUIPlugin.getDefault().getPlaylist());
		KayleeUIPlugin.getDefault().addListener(this);
	}

	
	public void dispose(){
		KayleeUIPlugin.getDefault().removeListener(this);
	}
	
	public void showPlaylist(AbstractPlaylist<?> playlist){
		setPartName(playlist.getName());
		viewer.setInput(playlist);
		viewer.refresh();
	}
	
	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

	
	protected void hookMouseListener(){
		viewer.addDoubleClickListener(new IDoubleClickListener(){

			@Override
			public void doubleClick(DoubleClickEvent event) {
				ISelection sel = event.getSelection();
				if (sel != null && sel instanceof IStructuredSelection){
					IStructuredSelection ssel = (IStructuredSelection) sel;
					IPlayable first = (IPlayable) ssel.getFirstElement();
					IPlayer player = KayleePlugin.getDefault().getPlayer();
					KayleeUIPlugin uiPlugin = KayleeUIPlugin.getDefault();
					IPlaylist playlist = uiPlugin.getPlaylist();
					uiPlugin.hookNavigationHandler(playlist);
					playlist.playOn(player, first);
				}
			}
			
		});
	}


	@Override
	public void playlistChanged(AbstractPlaylist<?> playlist) {
		showPlaylist(playlist);
	}

}
